<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/29
 * Time: 11:29
 */

namespace App\Http\Logics\Admin;


use App\Cafe;
use App\CashCoupon;
use App\Partner;
use App\UserMappingCashCoupon;
use Intervention\Image\Facades\Image;
use App\Http\Logics\BaseLogic;
class CashCouponLogic extends BaseLogic
{
    /**
     * 创建代金券
     * @param $data
     * @return mixed
     */
    public static function createCashCoupon($data)
    {
        $response = self::getPartnerIdByKey($data['key']);
        if ($response->code){
            return self::response($response->code);
        }
        $partner_id = $response->data;
        $cash_coupon = new CashCoupon();
        $cash_coupon->name = $data['name'];
        $cash_coupon->description = $data['description'];
        $cash_coupon->num = $data['num'];
        $cash_coupon->denomination = $data['denomination'];
        $cash_coupon->limit = $data['limit'];
        $cash_coupon->start_time = $data['start_time'];
        $cash_coupon->end_time = $data['end_time'];
        $cash_coupon->partner_id = $partner_id;
        if (!$cash_coupon->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }

        // 创建代金券小程序码
        $page = 'pages/module/certificate/front/detail/detail';
        $response = self::createMiniProgressQrcodeUnlimit($partner_id, $page, $cash_coupon->id);
        if ($response->code)
        {
            return self::response($response->code);
        }
        $cash_coupon->qrcode = $response->data;
        if (!$cash_coupon->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 获取代金券列表
     * @param $data
     * @return mixed
     */
    public static function getCashCouponList($data)
    {
        $response = self::getPartnerIdByKey($data['key']);
        if ($response->code){
            return self::response($response->code);
        }
        $partner_id = $response->data;
        $cash_coupon = CashCoupon::where('partner_id', $partner_id)->orderBy('id', 'desc')->get();

        if (empty($cash_coupon)){
            return self::response(self::CONTENT_IS_EMPTY);
        }

        foreach ($cash_coupon as $item){
            // 增加用户是否领过代金券状态
            if (!empty($data['loginSession'])){
                $response = self::getUserIdByLoginSession($data['loginSession']);
                if ($response->code){
                    return self::response($response->code);
                }
                $user_id = $response->data;
                $user_mapping_cash_coupon = UserMappingCashCoupon::where('user_id', $user_id)->where('cash_coupon_id', $item->id)->first();
                $item->is_get = empty($user_mapping_cash_coupon) ? false : true;
            }else{
                $item->is_get = false;
            }
        }

        return self::response(self::SUCCESS, $cash_coupon);
    }

    /**
     * 删除代金券
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public static function deleteCashCoupon($data)
    {
        $cash_coupon = CashCoupon::find($data['cash_coupon_id']);
        if (empty($cash_coupon)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$cash_coupon->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }


    /**
     * 核销代金券
     * @param $data 筛选条件
     * @return mixed
     */
    public static function destroyCashCoupon($data)
    {
        $cash_coupon = CashCoupon::find($data['cash_coupon_id']);
        if (empty($cash_coupon)){
            return self::response(self::CONTENT_IS_EMPTY);
        }

        // 判断代金券是否过期
        if ($cash_coupon->end_time < strtotime("+1 day")){
            return self::response(self::CASH_COUPON_IS_EXPIRED);
        }

        $user_mapping_cash_coupon = UserMappingCashCoupon::where('cash_coupon_id', $data['cash_coupon_id'])->where('user_id', $data['user_id'])->first();
        if (empty($user_mapping_cash_coupon)){
            return self::response(self::CONTENT_IS_EMPTY);
        }

        // 防止重复核销代金券
        if ($user_mapping_cash_coupon->is_used){
            return self::response(self::DESTROY_CASH_COUPON_AGAIN);
        }


        $user_mapping_cash_coupon->is_used = 1;
        if (!$user_mapping_cash_coupon->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function getCashCouponDetail($data)
    {
        $cash_coupon = CashCoupon::find($data['cash_coupon_id']);
        if (empty($cash_coupon)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $cash_coupon);
    }

    /**
     * 创建代金券分享海报
     * @param $data
     * @return mixed
     */
    public static function createCashCouponPoster($data)
    {
        $cash_coupon = CashCoupon::find($data['cash_coupon_id']);
        if (empty($cash_coupon)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $partner_id = $cash_coupon->partner_id;
        $cafe = Cafe::where('partner_id', $partner_id)->first();
        if (empty($cafe)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        // 创建画布
        $img = Image::make(public_path().'/image/coupon-share.jpeg');
        $qrcode = downloadImage($cash_coupon->qrcode.'?imageMogr2/thumbnail/120x');
        $img = $img->insert($qrcode, 'top', 0, 510);
        // 将使用过的图片从服务器本地移除
        removeImage($qrcode);
        $name_arr = ch2arr($cafe->name, 14);
        // 根据咖啡馆名称的长度调控咖啡馆名称字体大小
        $font_size = 48-count($name_arr)*6;
        $img->text($cafe->name, 300, 360, function($font) use ($font_size) {
            $font->file(public_path().'/font/weiruanyahei.ttf');
            $font->size($font_size);
            $font->align('center');
            $font->valign('top');
            $font->color('#000000');
        });
        $img->save('/temp/cash-coupon-'.$cash_coupon->id.'-poster.jpg', 100);
        $cafe_poster_url = uploadImage('/temp/cash-coupon-'.$cash_coupon->id.'-poster.jpg', 'photos');
        // 将图片从本地服务器删除
        removeImage('/temp/cash-coupon-'.$cash_coupon->id.'-poster.jpg');
        return self::response(self::SUCCESS, $cafe_poster_url);
    }
}